package com.itheima.reggie.controller;

import cn.hutool.core.util.ReUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    ShoppingCartService shoppingCartService;

    //    添加购物车
    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart) {
        log.info("购物车数据:{}", shoppingCart);
        //获取用户id
        Long userId = BaseContext.getCurrentId();

        LambdaQueryWrapper<ShoppingCart> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(ShoppingCart::getUserId, userId);
        //获取id
        Long dishId = shoppingCart.getDishId();
        //判断菜品还是套餐
        if (dishId != null) {
            //    点的是菜品
            lqw1.eq(ShoppingCart::getDishId, dishId);
        } else {
            //    点的是套餐
            lqw1.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //查寻有没有购物车
        ShoppingCart one = shoppingCartService.getOne(lqw1);
        //判断,有就更新，没有就添加
        if (one == null) {
            //    默认购物车数量为一
            //设置用户id
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(BaseContext.getCurrentId());
            //添加购物车
            shoppingCartService.save(shoppingCart);
        } else {
            //    存在就加一
            one.setNumber(one.getNumber() + 1);
            shoppingCartService.updateById(one);

        }

        //ShoppingCart byId = shoppingCartService.getById(1570747422940934146L);
        return R.success("添加成功");
    }

    //查询购物车
    @GetMapping("/list")
    public R list() {
        log.info("查看购物车...");

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);

        return R.success(list);



        /*ArrayList<Object> objects = new ArrayList<>();
        return R.success(objects);*/
    }

    //    取消购物车
    @PostMapping("/sub")
    public R sub(@RequestBody ShoppingCart shoppingCart) {
        //添加条件
        LambdaUpdateWrapper<ShoppingCart> up = new LambdaUpdateWrapper<>();
        //用户id，防止删除全部数据
        Long userId = BaseContext.getCurrentId();
        up.eq(ShoppingCart::getUserId, userId);
        //获取菜品id
        Long dishId = shoppingCart.getDishId();
        //判断菜品还是套餐，添加查寻条件
        if (dishId != null) {
            up.eq(ShoppingCart::getDishId, dishId);
        } else {
            up.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //查出数据
        ShoppingCart one = shoppingCartService.getOne(up);
        Integer number = one.getNumber();
        number--;
        //判断one是否为空
        if (number > 0) {
            shoppingCartService.updateById(one);
        } else {
            shoppingCartService.remove(up);
        }
        return R.success(one);
    }

//    清空购物车
    @DeleteMapping("/clean")
    public  R clean(){
        //获取当前用户id
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userId);
        shoppingCartService.remove(lqw);
        return R.success("清空成功");
    }
}
